package com.itheima.controller;

import com.itheima.annotation.LimitOperate;
import com.itheima.common.PageBean;
import com.itheima.common.Result;
import com.itheima.pojo.Emp;
import com.itheima.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;

/**
 * 员工管理Controller
 */
@Slf4j
@RestController
@RequestMapping("/emps")
public class EmpController {
    private final EmpService empService;


    /**
     * Instantiates a new Emp controller.
     *
     * @param empService the emp service
     */
    public EmpController(EmpService empService) {
        this.empService = empService;
    }

    /**
     * Page result.
     *
     * @param name     the name
     * @param gender   the gender
     * @param begin    the begin
     * @param end      the end
     * @param page     the page
     * @param pageSize the page size
     * @return the result
     */
    @GetMapping
    public Result<PageBean<Emp>> page(String name,
                                      Short gender,
                                      @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
                                      @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end,
                                      @RequestParam(defaultValue = "1") Integer page,
                                      @RequestParam(defaultValue = "1") Integer pageSize) {

        PageBean<Emp> pageBean = empService.page(name, gender, begin, end, page, pageSize);
        return Result.success(pageBean);
    }

    /**
     * Remove batch result.
     *
     * @param ids the ids
     * @return the result
     */
    @DeleteMapping("/{ids}")
    @LimitOperate
    public Result<?> removeBatch(@PathVariable List<String> ids){
        empService.removeBatch(ids);
        return Result.success();
    }

    /**
     * Create result.
     *
     * @param emp the emp
     * @return the result
     */
    @PostMapping
    @LimitOperate
    public Result<?> create(@RequestBody Emp emp){
        empService.create(emp);
        return Result.success();
    }

    /**
     * Query by id result.
     *
     * @param id the id
     * @return the result
     */
    @GetMapping("/{id}")
    public Result<Emp> queryById(@PathVariable Integer id){
       Emp emp =  empService.queryById(id);
       return Result.success(emp);
    }

    /**
     * Update result.
     *
     * @param emp the emp
     * @return the result
     */
    @PutMapping
    @LimitOperate
    public Result<?> update(@RequestBody Emp emp){
        empService.update(emp);
        return Result.success();
    }


}
